package com.neusoft.bizcore.notice.system.converter;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.notice.system.dto.SystemNoticeDTO;
import com.neusoft.bizcore.notice.system.model.SystemNotice;
import com.neusoft.bizcore.notice.system.model.enums.SystemNoticeStatus;
import com.neusoft.bizcore.notice.system.repository.SystemNoticeRepository;
import com.neusoft.bizcore.web.dto.AbstractConverter;

@Component
public class SystemNoticeConverter extends AbstractConverter<SystemNotice, SystemNoticeDTO> {

    @Autowired
    private SystemNoticeRepository messageRepository;

    @Autowired
    private NoticeConnectUserConverter noticeConnectConverter;

    @Override
    public SystemNotice toModel(final SystemNoticeDTO dto) {
        SystemNotice model;
        if (dto.isNew()) {
            model = new SystemNotice();
        } else {
            model = this.messageRepository.getOne(dto.getId());
        }

        model.setAppCode(dto.getAppCode());
        model.setIsRead(!StringUtils.isEmpty(dto.getIsRead()) ? SystemNoticeStatus.valueOf(dto.getIsRead())
                : SystemNoticeStatus.unRead);
        model.setNoticeType(StringUtils.isEmpty(dto.getNoticeType()) ? "基本通知" : dto.getNoticeType());
        model.setTitle(dto.getTitle());
        model.setContent(dto.getContent());
        model.setBusinessInfo(dto.getBusinessInfo());
        model.setNoticeRoute(dto.getNoticeRoute());
        model.setUser(this.noticeConnectConverter.toModel(dto.getUser()));

        return model;
    }

    @Override
    public SystemNoticeDTO toDTO(final SystemNotice model, final boolean forListView) {

        final SystemNoticeDTO dto = new SystemNoticeDTO();

        dto.setId(model.getId());
        dto.setAppCode(model.getAppCode());
        dto.setIsRead(model.getIsRead().toString());
        dto.setNoticeType(model.getNoticeType().toString());
        dto.setTitle(model.getTitle());
        dto.setContent(model.getContent());
        dto.setBusinessInfo(model.getBusinessInfo());
        dto.setUser(this.noticeConnectConverter.toDTO(model.getUser()));
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());
        dto.setNoticeRoute(model.getNoticeRoute());
        return dto;
    }
}
